package cn.hahah.module.blog.service.visithistorybrowse;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.visithistorybrowse.vo.*;
import cn.hahah.module.blog.dal.dataobject.visithistorybrowse.VisitHistoryBrowseDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.visithistorybrowse.VisitHistoryBrowseMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 游客文章浏览记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VisitHistoryBrowseServiceImpl implements VisitHistoryBrowseService {

    @Resource
    private VisitHistoryBrowseMapper visitHistoryBrowseMapper;

    @Override
    public Long createVisitHistoryBrowse(VisitHistoryBrowseSaveReqVO createReqVO) {
        // 插入
        VisitHistoryBrowseDO visitHistoryBrowse = BeanUtils.toBean(createReqVO, VisitHistoryBrowseDO.class);
        visitHistoryBrowseMapper.insert(visitHistoryBrowse);
        // 返回
        return visitHistoryBrowse.getId();
    }

    @Override
    public void updateVisitHistoryBrowse(VisitHistoryBrowseSaveReqVO updateReqVO) {
        // 校验存在
        validateVisitHistoryBrowseExists(updateReqVO.getId());
        // 更新
        VisitHistoryBrowseDO updateObj = BeanUtils.toBean(updateReqVO, VisitHistoryBrowseDO.class);
        visitHistoryBrowseMapper.updateById(updateObj);
    }

    @Override
    public void deleteVisitHistoryBrowse(Long id) {
        // 校验存在
        validateVisitHistoryBrowseExists(id);
        // 删除
        visitHistoryBrowseMapper.deleteById(id);
    }

    private void validateVisitHistoryBrowseExists(Long id) {
        if (visitHistoryBrowseMapper.selectById(id) == null) {
            throw exception(VISIT_HISTORY_BROWSE_NOT_EXISTS);
        }
    }

    @Override
    public VisitHistoryBrowseDO getVisitHistoryBrowse(Long id) {
        return visitHistoryBrowseMapper.selectById(id);
    }

    @Override
    public PageResult<VisitHistoryBrowseDO> getVisitHistoryBrowsePage(VisitHistoryBrowsePageReqVO pageReqVO) {
        return visitHistoryBrowseMapper.selectPage(pageReqVO);
    }

}